/**
 * 
 */
package hu.cubussapiens.facts.query;

import java.util.ArrayList;
import java.util.List;

/**
 * @author balage
 *
 */
public class And implements Query {

	private final List<Query> queries;
	
	/**
	 * 
	 */
	public And(List<Query> queries) {
		this.queries = queries;
	}

	/* (non-Javadoc)
	 * @see hu.cubussapiens.facts.query.Query#execute(hu.cubussapiens.facts.query.IQueryContext)
	 */
	@Override
	public List<Long> execute(IQueryContext context) {
		List<Long> result = null;
		
		for(Query q : queries){
			if (result == null)
				result = q.execute(context);
			else{
				List<Long> r = q.execute(context);
				List<Long> w = new ArrayList<Long>();
				for(Long l : result)
					if (r.contains(l)) w.add(l);
				result = w;
			}
		}
		
		return (result == null) ? new ArrayList<Long>() : result;
	}

}
